import Dialog from '../../../../dist/dialog/dialog';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      category_name: "",
      content: "",
      title: "",
      is_headline: false,
      pic: "",
    },
    img_url: "",
    category_show: false,
    meal_show: false,
    type: "",
    id: "",
    headline_fee: 0,
    currentPackage: {},
    classifyData: [],
    order_id: "",
    is_upload: true,
    is_show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,'optionsoptions');
    this.setData({
      type: options ? options.type : "",
      id: options ? options.id : ""
    })
    if(this.data.type == "check") {
      this.getNeedDetail();
    } else {
      this.getClassifyData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.data.type == "check") {
      // this.getNeedDetail();
    } else {
      this.getEnterprisePer();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  inputimg(event) {
    if(this.data.is_upload) {
      this.setData({
        'form.pic':event.detail ? event.detail[event.detail.length - 1] : "",
      })
    }else {
      this.setData({
        is_upload: true
      })
    }
  },
  delete(event) {
    this.setData({
      'form.pic': "",
      is_upload: false
    })
  },
  btnCategoryShow() {
    this.setData({
      category_show: !this.data.category_show
    })
  },
  chooseCategory(e) {
    let classifyData = this.data.classifyData; 
    for(let item of classifyData) {
      if(e.currentTarget.dataset.id == item.id) {
        item.ishow = true;
      }else {
        item.ishow = false;
      }
    }
    this.setData({
      classifyData
    })
  },
  goEnterprisePage() {
    // this.setData({
    //   meal_show: true
    // })
    wx.navigateTo({
      url:  `/packageJ/anytime_service/enterprise_application/enterprise_package/index`,
    });
  },
  getNeedDetail() {
    let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.enterprise.need-detail");
    app._postNetWork({
      url: baseUrl,
      data: { id: this.data.id },
      success: ({data : { data,result,msg }}) => {
        console.log(2222,data,result,msg);
        if (result !== 1) {
          app.tips(msg)
          return false;
        }
        this.setData({
          img_url: data.pic_url,
          'form.category_name': data.has_one_category.title,
          'form.title': data.title,
          'form.content': data.content,
          'form.is_headline': data.is_headline == 1 ? true : false,
          headline_fee: data.headline_fee,
          currentPackage: data.has_one_package,
          order_id: data.order_id
        })
      }
    });
  },
  // 获取分类
  getClassifyData() {
    let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.index.get-category-list");
    app._postNetWork({
      url: baseUrl,
      data: { sort_type: 3 },
      success: ({data : { data,result,msg }}) => {
        if (result !== 1) {
          app.tips(msg)
          return false;
        }
        this.setData({
          classifyData: data
        })
        // this.getEnterprisePer();
      }
    });
  },
  // 企业-发布需求前置接口
  getEnterprisePer() {
    let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.create-enterprise-need.pre");
      app._postNetWork({
        url: baseUrl,
        data: {  },
        success: ({data : { data,result,msg }}) => {
          if (result !== 1) {
            app.tips(msg)
            return false;
          }
          this.setData({
            headline_fee: data.headline_fee,
            is_show: true
          })
          if(data.currentPackage) {
            this.setData({
              currentPackage: data.currentPackage,
            })
          }else {
            wx.navigateTo({
              url:  `/packageJ/anytime_service/enterprise_application/enterprise_package/index`,
            });
          }
        }
      });
  },
  onChangeCheckbox(event) {
    this.setData({
      'form.is_headline': event.detail,
    });
  },
  optionPackage(op) {
    console.log(op,'opop');
    this.setData({
      meal_show: false,
      currentPackage: op.detail[0]
    })
  },
  submit() {
    console.log(this.data.form.category_name,32323);
    if(this.data.type == "check") {
      wx.navigateTo({
        url:  `/packageJ/anytime_service/order/order_detail/index?type=enterprise&order_id=${this.data.order_id}`,
      });
    }else {
      let form = {
        is_headline: this.data.form.is_headline ? 1 : 0,
        category_id: this.data.form.category_name ? this.data.classifyData.filter(item => item.ishow).length > 0 ? this.data.classifyData.filter(item => item.ishow)[0].id : "" : "",
        title: this.data.form.title,
        content: this.data.form.content,
        pic: this.data.form.pic
      }
      console.log(form,'formform');
      // return
      let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.create-enterprise-need.index");
      app._postNetWork({
        url: baseUrl,
        data: form,
        success: ({data : { data,result,msg }}) => {
          console.log(2222,data,result,msg);
          if (result !== 1) {
            app.tips(msg)
            return false;
          }
          wx.navigateTo({
            url: '/packageD/buy/orderPay/orderPay?order_id=' + data.order_ids + '&status=2'
          });
        }
      });
    }
  },
  sureOption() {
    this.setData({
      category_show: false,
      'form.category_name': this.data.classifyData.filter(item => item.ishow)[0].title
    })
  },
  onChangeTitle(event) {
    this.setData({
      'form.title': event.detail
    })
  },
  onChangeContent(event) {
    this.setData({
      'form.content': event.detail
    })
  }
})